package com.example.testapp.activity.activity.study;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewStub;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.activity.SuperActivity;
import com.example.testapp.activity.util.FileUtils;
import com.example.testapp.activity.util.Utils;

import java.io.File;

/**
 * Created by l on 2016/9/1.
 */
public class SettingsActivity extends SuperActivity implements View.OnClickListener {

    private TextView  version;
    private TextView  cache;
    private ViewStub  cacheViewStub;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_settings);
         initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cache.setText(FileUtils.getDiskCacheSize(this));
    }

    private void initView() {
        RelativeLayout headView = (RelativeLayout) this.findViewById(R.id.head);
        RelativeLayout leftBack = (RelativeLayout) headView.findViewById(R.id.back_left);
        TextView  title = (TextView) headView.findViewById(R.id.head_title);
        leftBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.setText("设置");
        RelativeLayout checkUpdate = (RelativeLayout) this.findViewById(R.id.check_update);
        version = (TextView) this.findViewById(R.id.version);
        RelativeLayout clearCache = (RelativeLayout)this.findViewById(R.id.clear_cache);
        cache = (TextView) this.findViewById(R.id.cache);
        RelativeLayout  contact = (RelativeLayout) this.findViewById(R.id.contact_person);
        RelativeLayout  feedback = (RelativeLayout) this.findViewById(R.id.feed_back);
        RelativeLayout  about = (RelativeLayout) this.findViewById(R.id.about);
        RelativeLayout  wel = (RelativeLayout) this.findViewById(R.id.enter_wel);
        checkUpdate.setOnClickListener(this);
        clearCache.setOnClickListener(this);
        contact.setOnClickListener(this);
        feedback.setOnClickListener(this);
        about.setOnClickListener(this);
        wel.setOnClickListener(this);

        cache.setText(FileUtils.getDiskCacheSize(mContext));
        version.setText("当前版本V" + GetVersion(mContext));
    }

    /**
     *  取得版本号
     * @param context
     * @return
     */
    private String GetVersion(Context context) {
        try {
            PackageInfo manager = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return manager.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            return "Unknown";
        }
    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.back_left:
               finish();
               break;
           case R.id.check_update:
               showToast("已经更新到最新版本");
               break;
           case R.id.clear_cache:  //清除缓存
               if (cacheViewStub == null){
                  cacheViewStub = (ViewStub)this.findViewById(R.id.cache_view);
                  View  infalter = cacheViewStub.inflate();
                  TextView ok = (TextView)infalter.findViewById(R.id.ok_button);
                  TextView cancel = (TextView)infalter.findViewById(R.id.cancle_button);
                  ok.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View v) {
                          // 清除缓存图片代码
                          File file = Utils.createDefaultCacheDir(mContext);
                          delAllFile(file.getPath());
                          cache.setText(FileUtils.getDiskCacheSize(mContext));
                          cacheViewStub.setVisibility(View.GONE);
                      }
                  });
                   cancel.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View v) {
                           cacheViewStub.setVisibility(View.GONE);
                       }
                   });
               }
               cacheViewStub.setVisibility(View.VISIBLE);
               break;
           case R.id.contact_person: //联系开发人员
               Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:18229095647"));
               startActivity(intent);
               break;
           case R.id.feed_back:  //意见反馈
               Intent intent0 = new Intent(SettingsActivity.this, FeedBackActivity.class);
               startActivity(intent0);
               break;
           case R.id.about:  //关于
               Intent intent1 = new Intent(SettingsActivity.this, AboutActivity.class);
               startActivity(intent1);
               break;
           case R.id.enter_wel:
               Intent intent2 = new Intent(SettingsActivity.this, WelcomeActivity.class);
               startActivity(intent2);
               break;
           default:
               break;
       }
    }

    /**
     * 删除指定文件夹下所有文件
     *
     * @param path 文件夹完整绝对路径
     * @return
     */
    private boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()){ //判断文件是否存在
            return flag;
        }
        if (!file.isDirectory()){
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for(int i = 0; i < tempList.length; i++){
            if (path.endsWith(File.separator)){
                 temp = new File(path + tempList[i]);
            }else{
                 temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()){
                temp.delete();
            }
            if (temp.isDirectory()){
                delAllFile(path + "/" + tempList[i]); // 先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]); // 再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 删除文件夹
     *
     * @param folderPath  文件夹完整绝对路径
     */
    private void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete();// 删除空文件夹
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void obtainInfo() {

    }
}
